<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Groups extends Backend {

    public function __construct()
    {
        parent::__construct();

        $this->lang->load('users');        
        $this->data['page_home']  = lang('lang_account');
        $this->data['page_title']  = lang('lang_groups');

    }


	public function index()
	{
        if ( ! $this->ion_auth->logged_in() OR ! $this->ion_auth->is_admin())
        {
            redirect('auth/login', 'refresh');
        }
        else
        {
           // Title
			$this->data['meta_title'] = $this->data['page_title'];

			// Breadcrumbs
			$this->data['array_breadcrumbs'] = array(
				$this->data['page_home']  => 'backend',
				$this->data['page_title'] => 'backend/ionauth/groups'
			);


            $this->data['groups'] = $this->ion_auth->groups()->result();


            $this->data['content_header_title'] = content_header_title($this->data['page_title']);
			$this->data['content'] = 'ionauth/groups/index';

            /* Load Template */
            $this->render();

        }
    }
    
    
    function permission($id){
        
        if ( ! $this->ion_auth->logged_in() OR ! $this->ion_auth->is_admin())
        {
            redirect('auth', 'refresh');
        }
        
        // Title
        $this->data['meta_title'] = $this->data['page_title'];

        // Breadcrumbs
        $this->data['array_breadcrumbs'] = array(
            $this->data['page_home']  => 'backend',
            $this->data['page_title'] => 'backend/ionauth/groups',
            "用户组权限设置" => 'backend/ionauth/groups/permission'
        );
        
        $group = $this->ion_auth->group($id)->row();
        
        if( $this->input->post() )
        {
            foreach($this->input->post() as $k => $v)
            {
                if( substr($k, 0, 5) == 'perm_' )
                {
                    $permission_id  =   str_replace("perm_","",$k);
        
                    if( $v == "X" )
                        $this->ion_auth_acl->remove_permission_from_group($id, $permission_id);
                    else
                        $this->ion_auth_acl->add_permission_to_group($id, $permission_id, $v);
                }
            }
        
            redirect('backend/ionauth/groups/permission/'.$id, 'refresh');
        }
        $this->data['groupname']=$group->name;
        $this->data['messages'] = $this->ion_auth_acl->errors() ? $this->ion_auth_acl->errors() :  $this->ion_auth_acl->messages() ? $this->ion_auth_acl->messages():$this->session->flashdata('message');
        
        $this->data['permissions']            =   $this->ion_auth_acl->permissions('full', 'perm_key');
        $this->data['group_permissions']      =   $this->ion_auth_acl->get_group_permissions($id);
        
        $this->data['content_header_title'] = content_header_title($this->data['page_title'],"用户组权限设置");
        $this->data['content'] = 'ionauth/users/group_permissions';

        /* Load Template */
        $this->render();
        
    }

	public function create()
	{
		if ( ! $this->ion_auth->logged_in() OR ! $this->ion_auth->is_admin())
		{
			redirect('auth', 'refresh');
		}

         // Title
         $this->data['meta_title'] = $this->data['page_title'];

         // Breadcrumbs
         $this->data['array_breadcrumbs'] = array(
             $this->data['page_home']  => 'backend',
             $this->data['page_title'] => 'backend/ionauth/groups',
             "创建管理组" => 'backend/ionauth/groups/create'
         );


		/* Validate form input */
		$this->form_validation->set_rules('group_name', 'lang:create_group_validation_name_label', 'required|alpha_dash');

		if ($this->form_validation->run() == TRUE)
		{
			$new_group_id = $this->ion_auth->create_group($this->input->post('group_name'), $this->input->post('description'));
			if ($new_group_id)
			{
				$this->session->set_flashdata('message', $this->ion_auth->messages());
				redirect('backend/ionauth/groups', 'refresh');
			}
		}
		else
		{
            $this->data['message'] = (validation_errors() ? validation_errors() : ($this->ion_auth->errors() ? $this->ion_auth->errors() : $this->session->flashdata('message')));

			$this->data['group_name'] = array(
				'name'  => 'group_name',
				'id'    => 'group_name',
				'type'  => 'text',
                'class' => 'form-control',
				'value' => $this->form_validation->set_value('group_name')
			);
			$this->data['description'] = array(
				'name'  => 'description',
				'id'    => 'description',
				'type'  => 'text',
                'class' => 'form-control',
				'value' => $this->form_validation->set_value('description')
			);

            $this->data['content_header_title'] = content_header_title($this->data['page_title'],"创建管理组");
			$this->data['content'] = 'ionauth/groups/create';

            /* Load Template */
            $this->render();

		}
	}


	public function delete()
	{
        if ( ! $this->ion_auth->logged_in())
        {
            redirect('auth/login', 'refresh');
        }
        elseif ( ! $this->ion_auth->is_admin())
		{
            return show_error('You must be an administrator to view this page.');
        }
        else
        {
            $this->load->view('backend/ionauth/groups/delete');
        }
	}


	public function edit($id)
	{
		if ( ! $this->ion_auth->logged_in() OR ! $this->ion_auth->is_admin() OR ! $id OR empty($id))
		{
			redirect('auth', 'refresh');
		}

         // Title
         $this->data['meta_title'] = $this->data['page_title'];

         // Breadcrumbs
         $this->data['array_breadcrumbs'] = array(
             $this->data['page_home']  => 'backend',
             $this->data['page_title'] => 'backend/ionauth/groups',
             "编辑管理组" => 'backend/ionauth/groups/edit'
         );

        /* Variables */
		$group = $this->ion_auth->group($id)->row();

		/* Validate form input */
        $this->form_validation->set_rules('group_name', $this->lang->line('edit_group_validation_name_label'), 'required|alpha_dash');

		if (isset($_POST) && ! empty($_POST))
		{
			if ($this->form_validation->run() == TRUE)
			{
				$group_update = $this->ion_auth->update_group($id, $_POST['group_name'], ["description"=>$_POST['group_description'],"bgcolor"=>$_POST['group_bgcolor']]);

				if ($group_update)
				{
					$this->session->set_flashdata('message', $this->lang->line('edit_group_saved'));

                    /* IN TEST */
                    //$this->db->update('groups', array('bgcolor' => $_POST['group_bgcolor']), 'id = '.$id);
				}
				else
				{
					$this->session->set_flashdata('message', $this->ion_auth->errors());
				}

				redirect('backend/ionauth/groups', 'refresh');
			}
		}

        $this->data['message'] = (validation_errors() ? validation_errors() : ($this->ion_auth->errors() ? $this->ion_auth->errors() : $this->session->flashdata('message')));
        //$this->data['group']   = $group;

		$readonly = $this->config->item('admin_group', 'ion_auth') === $group->name ? 'readonly' : '';

		$this->data['group_name'] = array(
			'type'    => 'text',
			'name'    => 'group_name',
			'id'      => 'group_name',
			'value'   => $this->form_validation->set_value('group_name', $group->name),
            'class'   => 'form-control',
			$readonly => $readonly
		);
		$this->data['group_description'] = array(
			'type'  => 'text',
			'name'  => 'group_description',
			'id'    => 'group_description',
			'value' => $this->form_validation->set_value('group_description', $group->description),
            'class' => 'form-control'
		);
		$this->data['group_bgcolor'] = array(
			'type'     => 'text',
			'name'     => 'group_bgcolor',
			'id'       => 'group_bgcolor',
			'value'    => $this->form_validation->set_value('group_bgcolor', $group->bgcolor),
			'data-src' => $group->bgcolor,
            'class'    => 'form-control'
		);

        $this->data['content_header_title'] = content_header_title($this->data['page_title'],"编辑管理组");
        $this->data['content'] = 'ionauth/groups/edit';

        /* Load Template */
        $this->render();
	}
}
